package com.whl.sm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class BaseController {
    @Autowired
    HttpServletRequest req;


    /**
     * 获取参数
     * @return
     */
    public QueryWrapper getLikeParam(String likeColumn) {
        QueryWrapper queryWrapper = new QueryWrapper();
        String likeName = ServletRequestUtils.getStringParameter(req, "likeName", "");
        if (!likeName.equals("undefined") && !likeName.equals("")){
            queryWrapper.like(likeColumn,likeName);
        }
        return queryWrapper;
    }

    /**
     * 获取页面
     * @return
     */
    public Page getPage() {
        int pageNum = ServletRequestUtils.getIntParameter(req, "pageNum", 1);
        int pageSize = ServletRequestUtils.getIntParameter(req, "pageSize", 10);

        return new Page(pageNum, pageSize);
    }

    /**
     * 需要的参数方式
     *
     * @param params     获取的参数集合
     * @param needParams 需获取参数的key
     */
    public void setHttpRequestParams(Map<String, Object> params, String... needParams) {
        if (needParams != null && needParams.length > 0) {
            for (String s : needParams) {
                String d = req.getParameter(s);
                if ( d!=null && !"null".equalsIgnoreCase(d)) {
                    params.put(s, d);
                }
            }
        }
    }

    /**
     * 需要的参数方式
     *
     * @param queryWrapper   查询queryWrapper;
     * @param needParams 需获取参数的key
     */
    public void setHttpRequestQueryWrapper(QueryWrapper queryWrapper, String... needParams) {
        if (needParams != null && needParams.length > 0) {
            for (String s : needParams) {
                String d = req.getParameter(s);
                if ( d!=null && !d.equals("undefined") && !d.equals("")) {
                    queryWrapper.eq(s, d);
                }
            }
        }
    }


    protected void downloadFileByPath(String fileName, String filePath, HttpServletResponse response) throws IOException {
        OutputStream os = null;
        InputStream fis = null;
        //打开文件输入流 和 servlet输出流
        try {
            fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
            fis = new FileInputStream(filePath);
            os = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            response.setContentType("application/octet-stream");
            byte[] buf = new byte[1024];
            int l = -1;
            while ((l = fis.read(buf)) != -1) {
                os.write(buf, 0, l);
                os.flush();
            }
        } catch (Exception e) {
            log.error("下载模板异常",e);
            response.setHeader("Content-Disposition", null);
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.flush();
            printWriter.println("<script language='javascript'>");
            printWriter.println("parent.window.alert('系统开小差了');");
            printWriter.println("</script>");
        } finally {
            //关闭流
            if (Objects.nonNull(os)) {
                os.close();
            }
            if (Objects.nonNull(fis)) {
                fis.close();
            }
        }
    }
}
